<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name$lk" placeholder="标题" class="filter-item" />
      <!-- <el-select v-model="listQuery.type" placeholder="类型" class="filter-item" v-if="type !== 1">
        <el-option v-for="item in typeImgsJson" :key="item.value" :label="item.label" :value="item.value" />
      </el-select> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"
        >
        查询
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="toSave(0)"
        >
        添加
      </el-button>
      <el-button class="filter-item" type="danger" icon="el-icon-delete" @click="handlePlDelete"
        >
        批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="序号" type="index" align="center" width="60px" />
      <el-table-column label="标题" prop="name" align="center" />
      <!-- <el-table-column label="浏览量" prop="num" align="center" v-if="type === 1" /> -->
      <!-- <el-table-column label="编号" prop="identifier" align="center" /> -->
      <el-table-column label="类型" prop="typeText" align="center" />
      <el-table-column label="图片" align="center" v-if="type !== 1">
        <template slot-scope="{row}">
          <el-image style="width: 100px; height: 100px" v-if="row.image" :src="row.image"
            :preview-src-list="[row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="toSave(row.id)" >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { shopPageByQo, shopRemoveByIds } from '@/api/shop'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { typeImgsJson } from '../../constant'
export default {
  name: 'ShopList',
  directives: { waves },
  components: { Pagination },
  props: {
    type: {
      default: 1,
      type: Number
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        name$lk: '',
        type: ''
      },
      multipleSelection: [],
      typeImgsJson
    }
  },
  created() {
    this.listQuery.type = this.type
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      shopPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    toSave(id) {
      this.$router.push({ path: `/shop/${this.type === 1 ? 'news' : 'imgs'}/save`, query: { id: id } })
    },
    // 批量操作
    handleSelectionChange(val) {
      this.multipleSelection = val.map((ele) => {
        return ele.id
      })
    },
    handlePlDelete() {
      if (!this.multipleSelection || this.multipleSelection.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      const params = {
        ids: this.multipleSelection.join(',')
      }
      this.$confirm('是否继续删除？').then(() => {
        shopRemoveByIds(params).then(() => {
          // this.$notify({
          //   title: '成功',
          //   message: '批量删除成功！',
          //   type: 'success',
          //   duration: 2000
          // })

          this.$message({
            message: '批量删除成功！',
            type: 'success'
          });
          this.getList()
        })
      })
    }
  }
}
</script>
